package com.ctgu.chatapp.Service

import android.util.Log
import com.ctgu.chatapp.Netty.NettyClient
import com.ctgu.chatapp.TaskTimerManager
import com.ctgu.chatapp.utils.MessageUtils

object RequestService {

    val TAG = "RequestService"

    fun sendLoginRequest(chatID: Long, password: String) {
        val request = MessageUtils.getLoginRequest(chatID, password)
        Log.d(TAG, "sendLoginRequest: $request")
        NettyClient.instance.channel!!.writeAndFlush(request)
        TaskTimerManager.add(request)
    }

    fun sendFriendRequest(uid: Long, touid: Long, type: String) {
        val request = MessageUtils.getFriendRequest(uid, touid, type)
        NettyClient.instance.channel!!.writeAndFlush(request)
        TaskTimerManager.add(request)
    }

    fun sendFriendResponse(uid: Long, touid: Long, state: Int, type: String) {
        val response = MessageUtils.getFriendResponse(uid, touid, type, state)
        NettyClient.instance.channel!!.writeAndFlush(response)
        TaskTimerManager.add(response)
    }

    fun sendChatRequest(uid: Long, touid: Long, context: String) {
        val singleChat = MessageUtils.getSingleChatRequestText(context, uid, touid)
        NettyClient.instance.channel!!.writeAndFlush(singleChat)
        TaskTimerManager.add(singleChat)
    }

    fun sendControlRequest(uid: Long, touid: Long, snowFlowId: Long) {
        val request = MessageUtils.getControlRequest(uid, touid, snowFlowId)
        NettyClient.instance.channel!!.writeAndFlush(request)
        TaskTimerManager.add(request)
    }

    fun sendAck(snowFlower: Long) {
        println("需要撤回的消息的ID为 $snowFlower")
        val request = MessageUtils.getAck(snowFlower)
        NettyClient.instance.channel!!.writeAndFlush(request)
    }
}